<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Hello React</title>
    <script src="https://cdn.staticfile.org/react/16.13.1/umd/react.development.js"></script>
    <script src="https://cdn.staticfile.org/react-dom/16.13.1/umd/react-dom.development.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <div id="example2"></div>
    <script type="text/babel">
      function MyH1(props) {
        return (
          <h1 style={{ color: "red" }}>
            {props.children}|{props.data}
          </h1>
        );
      }
      class MyH2 extends React.Component {
        constructor(props) {
          super(props);
          this.state = { num: 1 };
          setInterval(() => {
            this.setState({ num: this.state.num + 1 });
          }, 1000);
        }
        render() {
          return <h2 style={{ color: "green" }}>{this.state.num}</h2>;
        }
      }
      class MyH3 extends React.Component {
        constructor(props) {
          super(props);
          // 给state赋初始值
          this.state = { a: 10 };
          console.log("构造器");
        }
        static getDerivedStateFromProps(props, state) {
          // 渲染之前修改state的值，如根据props改变state
          console.log("getDerivedStateFromProps来了", props, state);
          return { b: 10 };
        }
        render() {
          // 正式的渲染
          console.log("render方法", this.props, this.state);
          return (
            <h3 style={{ color: "green" }}>
              {this.props.children}|||{this.state.c}
            </h3>
          );
        }
        componentDidMount() {
          // 请求数据，和一些异步的操作
          console.log("组件挂载好了");
          console.log("组件刷新一次");
          this.setState({ a: 10 });
        }
        shouldComponentUpdate(newProps, newState) {
          console.log("shouldComponentUpdate", newProps, newState);
          console.log("当前state是", this.state);
          // 是否要render，如果false，后面的流程就停了，比如前后状态一样，就不需要重新渲染，可以提高性能
          return this.state.a != newState.a;
          // return false;
        }
        getSnapshotBeforeUpdate(prevProps, prevState) {
          console.log("getSnapshotBeforeUpdate", prevProps, prevState);
          return "sssssssss";
        }
        componentDidUpdate(prevProps, prevState, s) {
          console.log("componentDidUpdate", prevProps, prevState, s);
        }
      }

      class Child extends React.Component {
        handleClick() {
          this.props.getContent("你不好");
        }

        render() {
          return (
            <p onClick={this.handleClick.bind(this)}>
              Child! {this.props.content}
            </p>
          );
        }
      }

      class Father extends React.Component {
        constructor(props) {
          super(props);
          this.state = { a: "你好" };
        }

        getContent(data) {
          this.state.a = data;
          this.setState({ a: data });
        }

        render() {
          return (
            <div>
              <h1>Father!{this.state.a}</h1>
              <Child
                getContent={this.getContent.bind(this)}
                content={"你好"}
              ></Child>
            </div>
          );
        }
      }

      ReactDOM.render(
        <div>
          <MyH1 data={"Hello"}>Hello,world!</MyH1>
          <MyH2>Hello,world!</MyH2>
          <MyH3>Hello,world!</MyH3>
        </div>,
        document.getElementById("example")
      );
      ReactDOM.render(<Father></Father>, document.getElementById("example2"));
    </script>
  </body>
</html>
